var index = 0
var textArray = new Array()
var textType = new Array()

const MUSIC_TYPE = {
  HEADER: "Header",
  INTRO: "Intro",
  VERSE: "Verse",
  CHORUS: "Chorus",
  INTERLUDE: "Interlude",
  OUTRO: "Outro"
}

const ChordType = {
  OTHER: "OTHER",
  BLOCK_BEGIN: "BLOCK_BEGIN",
  BLOCK_END: "BLOCK_END",

  CHORD_BEGIN: "CHORD_BEGIN",
  CHORD_END: "CHORD_END",

  HEAD_BEGIN: "HEAD_BEGIN",
  HEAD_END: "HEAD_END",

  INTRO_BEGIN: "INTRO_BEGIN",
  INTRO_END: "INTRO_END",

  VERSE_BEGIN: "VERSE_BEGIN",
  VERSE_END: "VERSE_END",

  CHORUS_BEGIN: "CHORUS_BEGIN",
  CHORUS_END: "CHORUS_END",

  INTERLUDE_BEGIN: "INTERLUDE_BEGIN",
  INTERLUDE_END: "INTERLUDE_END",

  OUTRO_BEGIN: "OUTRO_BEGIN",
  OUTRO_END: "OUTRO_END",

  CHORD: "CHORD",
  BLANK_LINE: "BLANK_LINE",
  INFO: "INFO"
}

module.exports = function parseBlock(content) {
  let preContent = Buffer.from(content, 'base64').toString()
  let contentList = preContent.split('\n');
  let blockBegin = false, blockEnd = false

  contentList.forEach((line) => {
    if (line.startsWith('```') && !blockBegin) {
      let text = line.substring(3)
      blockBegin = true
      textType.push(ChordType.BLOCK_BEGIN)
      textArray.push(text)
    } else if (line.startsWith('```') && !blockEnd) {
      blockEnd = false
      blockBegin = false
      textArray.push(" ")
      textType.push(ChordType.BLOCK_END)
    } else {
      textArray.push(line)
      textType.push(ChordType.OTHER)
    }
  })
  deinfeLineType()
  defineHtmlLabel()
  let htmlString = ''
  textArray.forEach((text, index) => {
    console.log(text)
    htmlString += text
  })
  return htmlString
  // console.log(textArray.length, '||', textType.length)
}

function deinfeLineType() {
  let isBlock = false
  let text = ''
  textArray.forEach((line, index) => {
    if (line.trim() == '' && textType[index] != ChordType.BLOCK_BEGIN && textType[index] != ChordType.BLOCK_END) {
      textType[index] = ChordType.BLANK_LINE
      return false
    }
    if (line.startsWith('#')) {
      textType[index] = ChordType.INFO
      let info = line.substring(1)
      textArray[index] = info
      return false
    }
    if (textType[index] == ChordType.BLOCK_BEGIN && !isBlock) {
      isBlock = true
      text = line
      switch (text) {
        case MUSIC_TYPE.CHORUS:
          textType[index] = ChordType.CHORUS_BEGIN
          break;
        case MUSIC_TYPE.HEADER:
          textType[index] = ChordType.HEAD_BEGIN
          break;
        case MUSIC_TYPE.INTERLUDE:
          textType[index] = ChordType.INTERLUDE_BEGIN
          break;
        case MUSIC_TYPE.INTRO:
          textType[index] = ChordType.INTRO_BEGIN
          break
        case MUSIC_TYPE.OUTRO:
          textType[index] = ChordType.OUTRO_BEGIN
          break
        case MUSIC_TYPE.VERSE:
          textType[index] = ChordType.VERSE_BEGIN
          break
        default: break
      }
    } else if (textType[index] == ChordType.BLOCK_END && isBlock) {
      isBlock = false
      switch (text) {
        case MUSIC_TYPE.CHORUS:
          textType[index] = ChordType.CHORUS_END
          break;
        case MUSIC_TYPE.HEADER:
          textType[index] = ChordType.HEAD_END
          break;
        case MUSIC_TYPE.INTERLUDE:
          textType[index] = ChordType.INTERLUDE_END
          break
        case MUSIC_TYPE.INTRO:
          textType[index] = ChordType.INTRO_END
          break
        case MUSIC_TYPE.OUTRO:
          textType[index] = ChordType.OUTRO_END
          break
        case MUSIC_TYPE.VERSE:
          textType[index] = ChordType.VERSE_END
          break
        default: break
      }
      // text = ''
    }
  })
}

function defineHtmlLabel() {
  textArray.forEach((line, index) => {
    let chord = '', isChord = false
    if (textType[index] == ChordType.INFO) {
      textArray[index] = "<p>" + line + "</p>"
    }
    if (textType[index].includes('BEGIN')) {
      textArray[index] = "<div class='part' data-name=\'" + textArray[index] + "\'>"
    }
    if (textType[index].includes('END')) {
      textArray[index] = "</div>"
    }
    let normalText = '', chordText = ''
    Array.from(line).forEach((text, index) => {
      if (text == '[' && !isChord) {
        if (line[index - 1] && line[index - 1] != ']') {
          chordText = line[index - 1]
        }
        isChord = true
        chord += '<span class=\'plain\'>' + normalText + '</span>' + '<span class=\'chord\' data-chord=\''
        normalText = ''
      } else if (text == ']' && isChord) {
        chord += '\'>' + chordText + '</span> '
        isChord = false
      } else if (isChord) {
        chord += text
      } else if (line[index + 1] != '[') {
        normalText += text
      }
    })
    if (chord.trim() != '') {
      textArray[index] = '<div>' + chord + '</div>'
    }
  })
}

// function guid() {
//   function s4() {
//     return Math.floor((1 + Math.random()) * 0x10000)
//       .toString(16)
//       .substring(1);
//   }
//   return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
// }



